package com.shenda.api.entity;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

import com.swak.codec.Digests;
import com.swak.entity.IdEntity;
import com.swak.rxtx.codec.Encodes;
import com.swak.utils.time.DateTimes;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * 数据
 * 
 * @author lifeng
 */
@Setter
@Getter
@Accessors(chain = true)
public class Data extends IdEntity<String> implements Encodes {
	private static final long serialVersionUID = 1L;
	private String name;
	private String gyro;
	private String gyroName;// 陀螺仪简单名称
	private String startTime;// 开始时间 14
	private String endTime;// 结束时间 14
	private String cjms; // 采集模式 2
	private String cjpl;// 采集频率 4
	private String odr; // 采集ODR 4
	private Integer uploads; // 上传总数
	private Integer total; // 总长度
	private Long times; // 总时长
	private Integer frames; // 总帧数
	private Boolean finished; // 是否结束

	/**
	 * 解析时长
	 * 
	 * @return
	 */
	public Data parseTimes() {
		LocalDateTime startTime = DateTimes.parseDate(this.startTime, "yyyyMMddHHmmss");
		LocalDateTime endTime = DateTimes.parseDate(this.endTime, "yyyyMMddHHmmss");
		times = DateTimes.betweenTwoTime(startTime, endTime, ChronoUnit.SECONDS);
		return this;
	}

	/**
	 * 自定义ID
	 */
	@Override
	public String prePersist() {
		return this.id;
	}

	/**
	 * 自定义ID
	 * 
	 * @param gyro
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static String id(String gyro, String startTime, String endTime) {
		return Digests.md5(
				new StringBuilder().append(gyro).append(".").append(startTime).append(".").append(endTime).toString());
	}
}